﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.CoreDomain;
using Actuals.Business;
using Actuals.Common;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using System.Data;

namespace Actuals
{
    public partial class AddPublicHolidays : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            Response.AddHeader("Cache-control", "no-store, must-revalidate,private,no-cache");
            Response.AddHeader("Pragma", "no-cache");
            Response.AddHeader("Expires", "0");

            Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);

            if (!IsPostBack)
            {

                if (Request.QueryString["Mode"] != null)
                {
                    string _mode = Request.QueryString["Mode"].ToString();
                    ViewState["Mode"] = _mode;
                    if (_mode == "Edit")
                    {
                        string _id = Request.QueryString["Id"].ToString();
                        ViewState["Id"] = _id;
                        hidId.Value = _id;
                        PopulatePublicHolidays(int.Parse(_id));
                    }

                }

            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IPublicHoliday publicHoliday = new PublicHoliday();
                

                
                publicHoliday.Description = txtDescription.Text;

                string[] _date = txtHolidayDate.Text.Split('-');
                publicHoliday.HolidayDate = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));
                BusinessPublicHoliday businessPublicHoliday = new BusinessPublicHoliday();

                if (ViewState["Mode"].ToString() == "Add")
                {
                    
                    businessPublicHoliday.AddAndUpdatePublicHolidays(publicHoliday);
                    lblMsg.Text = "Saved";
                }
                else
                {
                    string _id = Request.QueryString["Id"].ToString();
                    ViewState["Id"] = _id;
                    publicHoliday.Id = int.Parse(ViewState["Id"].ToString());
                    businessPublicHoliday.AddAndUpdatePublicHolidays(publicHoliday);
                    lblMsg.Text = "Updated";
                }
            }
            catch (Exception ex)
            {

                lblMsg.Text = ex.Message;
            }


        }

        protected void PopulatePublicHolidays(int id)
        {
            IBusinessPublicHoliday businessPublicHolidays = new BusinessPublicHoliday();
            IPublicHoliday publicHolidys = businessPublicHolidays.GetPublicHoliday(id);
            txtDescription.Text = publicHolidys.Description;
            DateTime _date = publicHolidys.HolidayDate;
            txtHolidayDate.Text = _date.ToString("MM-dd-yyyy");




        }

    }
}